package com.dkm.nettysocketdemo.myWebsite.service.impl;

import com.dkm.nettysocketdemo.myWebsite.dao.HelmetClientMapper;
import com.dkm.nettysocketdemo.myWebsite.dao.HelmetModMapper;
import com.dkm.nettysocketdemo.myWebsite.domain.HelmetClient;
import com.dkm.nettysocketdemo.myWebsite.service.HelmetClientService;
import com.dkm.nettysocketdemo.untils.FileTools;
import com.dkm.nettysocketdemo.untils.Tools;
import com.dkm.nettysocketdemo.untils.redis.RedisOperator;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName : HelmetClientServiceImpl  //类名
 * @Description : 客户服务接口实现类  //描述
 * @Author : zmy  //作者
 * @Date: 2021-03-23 14:32  //时间
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HelmetClientServiceImpl implements HelmetClientService {
    /**
     * 用户mapper
     */
    @Resource
    HelmetClientMapper clientMapper;
    /**
     * 文件上传类
     */
    @Resource
    FileTools fileTools;
    /**
     * 安全帽Mappper
     */
    @Resource
    HelmetModMapper modMapper;
    /**
     * redis
     */
    @Resource
    RedisOperator redisOperator;
    Gson gson=new Gson();

    /**
     * id查询
     * @param pkClientId
     * @return
     */
    @Override
    public HelmetClient findById(String pkClientId) {
     HelmetClient client= gson.fromJson((String) redisOperator.get(pkClientId),HelmetClient.class);
        //判断为空时
        if(ObjectUtils.isEmpty(client)){
            //从数据库查询数据库
            client = clientMapper.selectByPrimaryKey(pkClientId);
            //将数据库数据存入redis
            redisOperator.set(pkClientId,gson.toJson(client));
        }
        return client;
    }

    /**
     * 修改
     * @param user 需要修改的用户对象
     * @return
     */
    @Override
    public int update(HelmetClient user) {
        if (!ObjectUtils.isEmpty(user.getClientPwd())){
            String password = Tools.password(user.getClientPwd());
            user.setClientPwd(password);
        }
        int row = clientMapper.updateByPrimaryKeySelective(user);
        if(row > 0){
            //从数据库查询数据库
            user = clientMapper.selectByPrimaryKey(user.getPkClientId());
            //将数据库数据存入redis
            redisOperator.set(user.getPkClientId(),gson.toJson(user));
        }
        return row;
    }

    /**
     * 个人更新
     * @param user 需要修改的用户对象
     * @param idss 安全帽id
     * @return  回调函数
     */
    @Override
    public int updateMod(HelmetClient user,List<String> idss) {
        // 判断用户密码是否修改
        if (!ObjectUtils.isEmpty(user.getClientPwd())){
            String password = Tools.password(user.getClientPwd());
            user.setClientPwd(password);
        }
        //查询用户之前的安全帽
        List<String> strings = modMapper.selectClientId(user.getPkClientId());
        if (!ObjectUtils.isEmpty(strings)){
            // 将安全帽的人员改为null
            modMapper.updateClient(strings,null);
        }
        if (idss!=null){
            // 将更改以后的数据加入数据库
            modMapper.updateClient(idss,user.getPkClientId());
        }
        // 存入redis
        int row = clientMapper.updateByPrimaryKeySelective(user);
        if (row>0) {
            HelmetClient client = clientMapper.selectByPrimaryKey(user.getPkClientId());
            //将数据存入redis
            redisOperator.set(user.getPkClientId(),gson.toJson(client));
        }
        return row;
    }

    /**
     * 新增
     * @param helmetClient 需要新增的客户对象
     * @return
     */
    @Override
    public int insert(HelmetClient helmetClient,List<String> idss) {
        helmetClient.setPkClientId(UUID.randomUUID().toString().replaceAll("-",""));
        // 密码加密
        helmetClient.setClientPwd(Tools.password(helmetClient.getClientPwd()));
        helmetClient.setIsRevision("1");
        // 安全帽集合是否为空
        if (!ObjectUtils.isEmpty(idss)){
            modMapper.updateClient(idss,helmetClient.getPkClientId());
        }
        int row = clientMapper.insertSelective(helmetClient);
        if (row>0){
            //将数据库数据存入redis
            redisOperator.set(helmetClient.getPkClientId(),gson.toJson(helmetClient));
        }
        return row;
    }

    /**
     * 判断登录
     * @param userAccountNumber 用户名称
     * @param userPwd 用户密码
     * @return
     */
    @Override
    public HelmetClient isLogin(String userAccountNumber, String userPwd) {
        // 密码加密
         userPwd=  Tools.password(userPwd);
        return clientMapper.isLogin(userAccountNumber,userPwd);
    }

    /**
     * 验证邮箱，修改邮箱为有效邮箱
     * @param userId 用户id
     * @return
     */
    @Override
    public int mailVerify(String userId) {
        return 0;
    }

    @Override
    public HelmetClient getMail(String mail) {
        return clientMapper.getMail(mail);
    }


    @Override
    public String updateFile(HelmetClient user) throws IOException {
        String path = fileTools.saveFile("head",user.getClientHeadPortraitFile());
        user.setClientHeadPortrait(path);
        int row =clientMapper.updateByPrimaryKeySelective(user);
        if(row==1){
            return path;
        }
        return null;
    }

    /**
     * 删除单条客户资料
     *
     * @param pkHelmetClientId
     * @return
     */
    @Override
    public int deleteById(String pkHelmetClientId) {
        //查询用户之前的安全帽
        List<String> strings = modMapper.selectClientId(pkHelmetClientId);
        if (!ObjectUtils.isEmpty(strings)){
            // 将安全帽的人员改为null
            modMapper.updateClient(strings,null);
        }
        int row = clientMapper.deleteByPrimaryKey(pkHelmetClientId);
        if (row>0) {
            redisOperator.del(pkHelmetClientId);
        }
        return row;
    }

    /**
     * 修改安全帽客户资料
     *
     * @param helmetClient
     * @return
     */
    @Override
    public int updateHelmetClient(HelmetClient helmetClient) {
        int row = clientMapper.updateByPrimaryKeySelective(helmetClient);
        if (row>0) {
            HelmetClient client = clientMapper.selectByPrimaryKey(helmetClient.getPkClientId());
            //将数据库数据存入redis
            redisOperator.set(helmetClient.getPkClientId(),gson.toJson(client));
        }
        return row;
    }

    /**
     * 查询所有安全帽客户列表
     *
     * @param helmetClient 客户
     * @param startTime 开始时间
     * @param endTime  结束时间
     * @param page 页数
     * @param limit 数量
     * @return 集合
     */

    @Override
    public List<HelmetClient> selectAllHelmetClient(HelmetClient helmetClient, String startTime, String endTime, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return clientMapper.selectAll(helmetClient,startTime,endTime);
    }

    /**
     * 查询客户总数
     *
     * @param helmetClient
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Integer rowCount(HelmetClient helmetClient, String startTime, String endTime) {
        return clientMapper.rowCount(helmetClient,startTime,endTime);
    }

    /**
     * 批量删除所有
     *
     * @param list
     * @return
     */
    @Override
    public int deleteAll(List<String> list, String name) {
        int row = clientMapper.deleteAll(list, name, new Date());
        if (row > 0) {
            redisOperator.del(list);
        }
        return row;
    }

    /**
     * 检查重名
     * @param clientAccountNumber 用户账号
     * @param pkClientId 客户id
     * @return
     */
    @Override
    public Integer selectCount(String clientAccountNumber, String pkClientId) {
        return clientMapper.selectCount(clientAccountNumber,pkClientId);
    }
}
